﻿using System;


namespace epicenter2010.Domain.Factory
{
	/// <summary>
	/// bstack @ 05/06/2010
	/// Order factory
	/// </summary>
	public class Order : epicenter2010.Domain.Factory.IOrder
	{
		/// <summary>
		/// Create
		/// </summary>
		/// <param name="newOrderId"></param>
		/// <param name="processOrderRequest"></param>
		/// <returns></returns>
		public epicenter2010.Domain.Entity.Order Create(
			int newOrderId,
			epicenter2010.Domain.Service.ProcessOrderRequest processOrderRequest)
		{
			var _order = new epicenter2010.Domain.Entity.Order(
				newOrderId,
				processOrderRequest.CustomerId,
				processOrderRequest.Description);

			_order.Initiate();

			if (!epicenter2010.Domain.Specification.OrderIsCandidateForOrderItemAddition.IsSatisfiedBy(_order))
				throw new Exception("Exception occurred when attempting to create order, status not 'Initiated'");

			_order.AddOrderItem(processOrderRequest.InitialOrderItem);

			return _order;
		}
	}
}
